<?
function customTimeBoxes() {
	$awesome= "
	<select name=\"hour\">";
	for($i=1;$i<=12;$i++) {
		$awesome.="
		<option value=\"".$i."\">".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
	}
	$awesome.= "
	</select>
	<select name=\"minute\">";
	for($i=0;$i<=59;$i++) {
		$awesome.= "
		<option value=\"".$i."\">".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
	}
	$awesome.= "
	</select>
	<select name=\"ampm\">
		<option value=\"0\">AM</option>
		<option value=\"1\">PM</option>
	</select>
	";
	return $awesome;
}


require_once("include.php");

$me=false;
require_once("header.session.php");
require_once("header.gui.php");
require_once("header.misc.php");


//globals
$MenuObjects= $me->getMyMenu();

if(!$me) {
	$me=&new Person($_SESSION['id']);
}

if(!isset($_GET['group'])) {
	$_GET['group']="default";
}
if(!isset($_GET['day'])) {
	$_GET['day']=unixToDate(time());
}
if(unixToDate(timestampToUnix($_GET['day']))!=$_GET['day']) {
	$_GET['day']=unixToDate(time());
}

if(isset($_GET['action'])) {
	$extraurl="";
	switch($_GET['action']) {
		case "absent":
			$times=getInOut($_GET['uid'],$_GET['day']);
			foreach($times as $val) {
				deleteInOut($val['attendance_id']);
			}
			$inid=studentIn($_GET['uid'],HMStoUnix(0,0,0,$_GET['day']));
			$extraurl="&viewaction=absent&id=" . $inid;
			break;
		case "editstatus":
			setExcused($_GET['id'],$_GET['excused'],$_GET['comments']);
			break;
		case "in":
			switch($_GET['time']) {
				case "now":
					if($_GET['day']==unixToDate(time())) {
						studentIn($_GET['id'],time());
					}
					break;
				case "custom":
					studentIn($_GET['uid'],HMStoUnix(($_GET['hour']+(12*($_GET['ampm']))),$_GET['minute'],0,$_GET['day']));
					break;
				case "ontime":
					setExcused(studentIn($_GET['id'],schoolStart($_GET['day']),$_GET['day']),1,"On Time");
					break;
			}
			break;
		case "out":
			switch($_GET['time']) {
				case "now":
					studentOut($_GET['id'],time());
					break;
				case "custom":
					studentOut($_GET['uid'],HMStoUnix(($_GET['hour']+(12*($_GET['ampm']))),$_GET['minute'],0,$_GET['day']));
					break;
			}
			break;
		case "delete":
			deleteInOut($_GET['id']);
			break;
		case "changedate":
			$_GET['day']=$_GET['year']."-".$_GET['month']."-".$_GET['dayofm'];
			break;
	}
	header("Location: " . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . $extraurl);
	die;
}

if(!isset($_GET['viewaction'])) {
	$_GET['viewaction']="default";
}

function Headers() {
	
}
function Body() {
	global $me;
	echo "
	<b>Current Date:</b> " . $_GET['day'] . "<br>
	";
	switch($_GET['viewaction']) {
		case "absent":
			echo "
			Please select either excused or unexcused...<br>
			<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"get\">
				<input type=\"hidden\" name=\"day\" value=\"" . $_GET['day'] . "\">
				<input type=\"hidden\" name=\"group\" value=\"" . $_GET['group'] . "\">
				<input type=\"hidden\" name=\"id\" value=\"" . $_GET['id'] . "\">
				<input type=\"hidden\" name=\"action\" value=\"editstatus\">
				<input type=\"radio\" name=\"excused\" value=\"0\">Unexcused<br>
				<input type=\"radio\" name=\"excused\" value=\"1\">Excused<br>
				Comments:<br>
				<textarea name=\"comments\">
				
				</textarea>
				<input type=\"submit\" value=\"Submit\">
			</form>
			";
			break;
		default:
			switch($_GET['group']) {
				case "advisory":
					$special="JOIN advisory_linker ON advisory_linker.people_id=people.people_id WHERE advisory_linker.advisory_id='" . $me->myAdvisory() . "'";
					break;
				case "all":
					$special="";
					break;
				default:
					$special="WHERE '1'='0'";
					break;
			}
			$special.=" ORDER BY people.people_lastname";
			//switch
			echo "
			<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"get\">
				<input type=\"hidden\" name=\"day\" value=\"" . $_GET['day'] . "\">
				<input type=\"hidden\" name=\"group\" value=\"" . $_GET['group'] . "\">
				<input type=\"hidden\" name=\"action\" value=\"changedate\">
				Month(MM): <input type=\"text\" name=\"month\">
				<br>
				Day(DD): <input type=\"text\" name=\"dayofm\">
				<br>
				Year(YYYY): <input type=\"text\" name=\"year\">
				<br>
				<input type=\"submit\">
			</form>
			<table border=\"1\" align=\"left\">
			<tr><th>Group By:</th></tr>
			<tr><td><a href=\"" . $_SERVER['PHP_SELF'] . "?group=all&day=" . $_GET['day'] . "\">All Students</a></td></tr>
			";
			if(isAdvisor($me->id)) {
				echo "
				<tr><td><a href=\"" . $_SERVER['PHP_SELF'] . "?group=advisory&day=" . $_GET['day'] . "\">Your Advisees</a></td></tr>
				";
			}
			
			echo "</table>";
			$users=getUserList($special);
			if(count($users)>0) {
				$students=array();
				foreach($users as $awesome) {
					if(isStudent($awesome['people_id'])) {
						$students[$awesome['people_id']]=$awesome;
					}
				}
				echo "<table border=\"1\">
				<tr><th>Student</th>
				<td width=\"155\">In</td><td width=\"155\">Out</td><td>Status</td></tr>
				";
				foreach($students as $fucks) {
					echo "
					<tr><td rowspan=\"2\">" . $fucks['people_firstname'] . " " . $fucks['people_lastname'] . "</td>
					<td>
					<a href=\"" . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . "&action=in&id=" . $fucks['people_id'] . "&time=now\">Now</a> | 
					<a href=\"" . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . "&action=in&id=" . $fucks['people_id'] . "&time=ontime\">On Time</a>
					</td><td>
					<a href=\"" . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . "&action=out&id=" . $fucks['people_id'] . "&time=now\">Now</a></td>
					<td rowspan=\"2\" width=\"250\">
					<table border=\"0\">
					";
					$awesome = getInOut($fucks['people_id'],$_GET['day']);
					foreach($awesome as $inout) {
						if($inout['attendance_in']==$_GET['day'] . " 00:00:00") {
							echo "
							<tr><td>Absent";
						} else {
							echo "
							<tr><td>In At: " . date("h:i",timestampToUnix($inout['attendance_in'])) . "</td>
							<td>
							";
							if($inout['attendance_out']!="0000-00-00 00:00:00") {
								echo "
									Out At: " . date("h:i",timestampToUnix($inout['attendance_out']));
							} else {
									echo " --- ";
							}
						}
//						echo "" . $inout['attendance_in'] . "!=" . unixToTimestamp(schoolStart($_GET['day']));
						if($inout['attendance_in']!=unixToTimestamp(schoolStart($_GET['day']))) {
							echo " 
								(<a href=\"" . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . "&viewaction=absent&id=" . $inout['attendance_id'] . "\">" . ($inout['attendance_excused']?"E":"U") . "</a>)
							";
						}
						echo "
						</td><td>
						<a href=\"" . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . "&action=delete&id=" . $inout['attendance_id'] . "\">Delete Entry</a>
						</td></tr>";
					}
					echo "
					</table>
					</td></tr>
					<tr><td>
					<form action=\"" . $_SERVER['PHP_SELF'] . "?\">
					<input type=\"hidden\" name=\"day\" value=\"" . $_GET['day'] . "\">
					<input type=\"hidden\" name=\"group\" value=\"" . $_GET['group'] . "\">
					<input type=\"hidden\" name=\"time\" value=\"custom\">
					<input type=\"hidden\" name=\"action\" value=\"in\">
					<input type=\"hidden\" name=\"uid\" value=\"" . $fucks['people_id'] . "\">
		
					" . customTimeBoxes() . "<br><input type=\"submit\" value=\"Custom In Time\">
					</form>
					<a href=\"" . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . "&action=absent&uid=" . $fucks['people_id'] . "\">Absent</a>
					</td>
					<td>
					<form action=\"" . $_SERVER['PHP_SELF'] . "?group=" . $_GET['group'] . "&day=" . $_GET['day'] . "&\">
					<input type=\"hidden\" name=\"group\" value=\"" . $_GET['group'] . "\">
					<input type=\"hidden\" name=\"day\" value=\"" . $_GET['day'] . "\">
					<input type=\"hidden\" name=\"time\" value=\"custom\">
					<input type=\"hidden\" name=\"action\" value=\"out\">
					<input type=\"hidden\" name=\"uid\" value=\"" . $fucks['people_id'] . "\">
					" . customTimeBoxes() . "<br><input type=\"submit\" value=\"Custom Out Time\"></form></td>
					</tr>";
				}
				echo "</table>";
			}
			break;
	}
}


include("template.php");